import { deepClone, generateId } from "./common";
import { ElMessage } from "element-plus";
import widgetJson from "../Design/SettingPanel/WidgetPanel/widget.json";
const widgetMap = {};
for (const widget of widgetJson) {
  if (widget.type === "Form") {
    for (const child of widget.children) {
      widgetMap[child.type] = child;
    }
    break;
  }
}
export type LayoutType = "tableLayout" | "dialogLayout" | "flowFormLayout" | "reportFormLayout";
export const createDesigner = (fields, fieldList) => {
  const layout: LayoutType = fields.layout;
  switch (layout) {
    case "tableLayout":
      const tableColumn = [];
      if (fieldList) {
        for (const field of fieldList) {
          tableColumn.push({
            id: "column" + generateId(),
            entityId: field.entityId,
            column: field.fieldCode,
            label: field.fieldName,
            dictType: field.dictType
          });
        }
      }
      return {
        layout: layout,
        selectedWidget: undefined,
        tableColumn: tableColumn,
        tableToolbar: [{ label: "新增", type: "primary", icon: "circle-plus", operType: "openDialog" }],
        tableOperation: {
          show: true,
          name: "操作",
          width: 100,
          num: 2
        },
        tableAction: [
          { label: "", type: "primary", icon: "edit", operType: "openDialog" },
          { label: "", type: "danger", icon: "delete", operType: "remove", popConfirm: "是否确认删除？" }
        ],
        tablePage: {
          pagination: true
        },
        widgetList: []
      };
    case "dialogLayout":
      const dialogWidgetList = [];
      if (fieldList) {
        for (const field of fieldList) {
          const widget = getWidget(field);
          dialogWidgetList.push(widget);
        }
      }
      return {
        layout: layout,
        selectedWidget: undefined,
        dialogProps: {
          id: null,
          title: fields.pageName,
          width: "750px",
          height: "400px",
          fullscreen: false,
          showFullScreen: false,
          modal: true
        },
        formProps: {
          labelWidth: 80,
          labelPosition: "left",
          size: "default",
          span: "24",
          disabled: false
        },
        widgetList: dialogWidgetList
      };
    case "flowFormLayout":
      const flowFormWidgetList = [];
      if (fieldList) {
        for (const field of fieldList) {
          const widget = getWidget(field);
          flowFormWidgetList.push(widget);
        }
      }
      return {
        layout: layout,
        selectedWidget: undefined,
        nodeName: "",
        buttonPermList: [],
        formProps: {
          labelWidth: 80,
          labelPosition: "left",
          size: "default",
          span: "24",
          disabled: false
        },
        widgetList: flowFormWidgetList
      };
    case "reportFormLayout":
      const reportFormWidgetList = [];
      if (fieldList) {
        for (const field of fieldList) {
          const widget = getWidget(field);
          reportFormWidgetList.push(widget);
        }
      }
      return {
        layout: layout,
        selectedWidget: undefined,
        nodeName: "",
        formProps: {
          labelWidth: 80,
          labelPosition: "left",
          size: "default",
          span: "24",
          disabled: false
        },
        widgetList: reportFormWidgetList
      };
    default:
      throw new Error("不支持的布局类型" + layout);
  }
};
export const getWidget = field => {
  let widget;
  if (field.dictType) {
    widget = deepClone(widgetMap["Select"]);
    widget.dictType = field.dictType;
  } else {
    switch (field.fieldType) {
      case "string":
        widget = deepClone(widgetMap["Input"]);
        break;
      case "number":
        widget = deepClone(widgetMap["Number"]);
        break;
      case "date":
        widget = deepClone(widgetMap["Date"]);
        break;
      case "bool":
        widget = deepClone(widgetMap["Switch"]);
        break;
      case "text":
        widget = deepClone(widgetMap["Input"]);
        widget.type = "area";
        break;
      default:
        ElMessage.warning("不支持字段类型" + field.fieldType);
        throw new Error("不支持字段类型" + field.fieldType);
        break;
    }
  }
  widget.id = widget.type + generateId();
  widget.formItemProps.prop = field.fieldCode;
  widget.name = widget.formItemProps.label = field.fieldName;
  return widget;
};
